package com.imu.chatmodule.LongConnection.handler;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.imu.chatmodule.LongConnection.handler.nettyService.HasChannelChatService;
import com.imu.chatmodule.LongConnection.handler.nettyService.NonChannelChatService;
import com.imu.chatmodule.LongConnection.model.LongConnectionChatDTO;
import com.imu.chatmodule.LongConnection.model.ReceiveMsgVo;
import com.imu.chatmodule.LongConnection.model.ReturnMsgVo;
import com.imu.chatmodule.LongConnection.model.SendMsgVo;
import com.imu.chatmodule.LongConnection.model.msg.FileMsg;
import com.imu.chatmodule.LongConnection.model.msg.FileMsgVo;
import com.imu.chatmodule.LongConnection.model.msg.TextMsg;
import com.imu.chatmodule.LongConnection.model.msg.TextMsgVo;
import com.imu.chatmodule.LongConnection.util.LongConnectionMsgUtil;
import com.imu.chatmodule.LongConnection.util.LongConnectionUtil;
import com.imu.chatmodule.ab.entity.Chatrecord;
import com.imu.chatmodule.ab.entity.FileData;
import com.imu.chatmodule.ab.service.IChatrecordService;
import com.imu.chatmodule.ab.service.IFileDataService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

/**
 * @author MaTaoxun
 * @package: com.imu.chatmodule.LongConnection.handler
 * @className: ChatHandler
 * @description: 聊天handler
 * @date 2021/9/28 23:05
 */

public class ChatHandler extends SimpleChannelInboundHandler<String> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

        ObjectMapper mapper = new ObjectMapper();
        LongConnectionChatDTO chatDTO = null;
        try {
            System.out.println(msg);
            chatDTO = mapper.readValue(msg, LongConnectionChatDTO.class);
            System.out.println(chatDTO);
        } catch (Exception e) {
            ctx.writeAndFlush(LongConnectionMsgUtil.ObjectToWSTextResult(new ReturnMsgVo(ReturnMsgVo.WRONG_PACKAGE, "数据包结构错误")));
            return;
        }
        // 未完成：需要检查投递的id是否存在

        SocketChannel channel = LongConnectionUtil.getChannelById(chatDTO.getReceiverId());
        if (channel == null) {
            String senderId = LongConnectionUtil.getIdByChannel((SocketChannel) ctx.channel());
            NonChannelChatService.doChat(chatDTO.getMsgType(),ctx,chatDTO,senderId);
        } else {
            String senderId = LongConnectionUtil.getIdByChannel((SocketChannel) ctx.channel());
            HasChannelChatService.doChat(chatDTO.getMsgType(),ctx,chatDTO,senderId);
        }
        ctx.fireChannelRead(msg);
    }


}
